/**
 * 25-Line ActionScript Contest Entry
 * 
 * Project: Tree generator
 * Author:  Edgars Simsons http://edzis.wordpress.com
 * Date:    21.11.2008.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
// 3 free lines! Alter the parameters of the following lines or remove them.
// Do not substitute other code for the three lines in this section
[SWF(width=800, height=800, backgroundColor=0xDBD3AE, frameRate=24)]
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

//	USAGE:	Click on the stage to reset

// 25 lines begins here!
var initObj:Object = {lengthRandom:0.4, level:1, thickness:1 };
var rootMc	:MovieClip = addChild(new MovieClip) as MovieClip;
rootMc.cacheAsBitmap = true;
rootMc.transform.matrix = new Matrix(-1, 0, 0, -1, Math.round(stage.stageWidth / 2), stage.stageHeight - 0);

stage.addEventListener(Event.ENTER_FRAME, redraw);
stage.addEventListener(MouseEvent.CLICK, doStart);
doStart();

function redraw(e:Event = null):void  { if(rootMc.thickness++ < 160) updateLine(rootMc, rootMc.thickness); }

function doStart(e:Event = null):void  { 
	for (var itemName:String in initObj) rootMc[itemName] = initObj[itemName];
	while(rootMc.numChildren > 0) rootMc.removeChildAt(0);
	rootMc.addChild(new Shape());
}

function updateLine(thisMc:MovieClip, thickness:int):MovieClip {
	thisMc.lineHeight = Math.round( thisMc.lengthRandom * (30* thisMc.thickness));
	thisMc.thickness = thickness;
	
	(thisMc.getChildAt(0) as Shape).graphics.clear();
	(thisMc.getChildAt(0) as Shape).graphics.lineStyle(thisMc.thickness/10, 0x1C6425);
	(thisMc.getChildAt(0) as Shape).graphics.lineTo( 0, thisMc.lineHeight / 10);
	
	if (thisMc.thickness > 5 && thisMc.level < 30) {
		if (thisMc.numChildren <= 1) for (var i:int = (thisMc.level <= 1)? 4 : 3; i >= 0; i--) {
			var shape:Shape = (thisMc.addChild(new MovieClip()) as MovieClip).addChild(new Shape()) as Shape;
			var configObj:Object = { lengthRandom: Math.round(5 + Math.random() * 2) / 10, thicknessRandom: (i == 0)? 7 :(i == 1) ? 3 + Math.random() * 3 : 3 + Math.random() * 5, thicknessMultiplication: Math.round(4 + Math.random() * 4) / 10, posRandom: (i == 0) ? 1 : ((i == 1) ? (0.8 - Math.random() * 0.2 * i) : (Math.round(3 + Math.random() * 3) / 10)),level: thisMc.level + 1, rotation: Math.round( (thisMc.level - 1) * -10 - 20  + Math.random() * (((thisMc.level - 1) * 20) + 40)) };
			for (var itemName:String in configObj) { shape.parent[itemName] = configObj[itemName] };
		}
		for (var j:int = thisMc.numChildren - 1; j >= 1; j--) updateLine((thisMc.getChildAt(j) as MovieClip), (((thisMc.thickness - 5) * (thisMc.getChildAt(j) as MovieClip).thicknessMultiplication) > 0.1)? ((thisMc.thickness - 5) * (thisMc.getChildAt(j) as MovieClip).thicknessMultiplication) : 0.1).y = thisMc.lineHeight/10 * (thisMc.getChildAt(j) as MovieClip).posRandom;
	}
	return thisMc;
}
// 25 lines ends here!